/*
 * Created on Mar 13, 2004
 */
package com.scalar.chiptrack.operations.packingslip.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.ClientInfo;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.packingslip.dao.NewPackingSlipDAO;
import com.scalar.chiptrack.operations.packingslip.forms.PackingSlipForm;
import com.scalar.chiptrack.sales.dao.SalesDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.dao.UserDAO;

/**
 * @author Ravikanth
**/
public class UpdatePackingSlipAction extends ChipTrackAction
{
	public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception 
	{
		HttpSession session = request.getSession();
		String returnVal 	= "failure";
		String action 		= request.getParameter( "action" );
		String currentUser 	= null; 
		boolean dropShip	= false;
		HashMap errors		= new HashMap();
		HashMap info		= new HashMap();
		HashMap lineItemMap 			= null;
		HashMap finalInventoryMap 		= null;
		int customerShippingId  		= -1;
		User user 	 					= null;
		PackingSlipForm packingSlipForm = (PackingSlipForm) form;
		
		errors 		= validateForm ( packingSlipForm, session );
 		user 		= (User) session.getAttribute ( SessionKeys.USER_KEY );
		currentUser = user.getUserId();

		if ( ( errors == null) || ( errors.isEmpty() == true ) )
		{
			try
			{
				finalInventoryMap = (HashMap) session.getAttribute ( SessionKeys.FINAL_INVENTORY_MAP );

				if( action.equalsIgnoreCase( "update" ) )
				{
					NewPackingSlipDAO.updateCustomerShippingInfo ( packingSlipForm, finalInventoryMap, currentUser );
					session.removeAttribute( SessionKeys.FINAL_INVENTORY_MAP );
					session.removeAttribute( "repetedPartNumber" );
                    request.setAttribute ( "termsAndConditions", SalesDAO.loadTermsAndConditionsAsList () );
                    returnVal = "packingSlipView";
				}
				else if ( action.equalsIgnoreCase( "updateInvNoAndAirwayBillNo" ) )
				{
					NewPackingSlipDAO.updateCustomerShippingInvNoAirwayBillno( packingSlipForm, currentUser );
					info.put( "Packing Slip ( " + packingSlipForm.getLabel() + " )", " updated successfully" );
					request.setAttribute( SessionKeys.INFO_MESSAGES, info );		
					returnVal = "packingSlipMenu";
				}
			}
			catch (ScalarException se)
			{
				// couldnt save customer_shipping information
				se.getRootCause().printStackTrace();
				errors.put ( "Could not save the packing slip", null );
				request.setAttribute( SessionKeys.ERROR_MESSAGES ,errors );
			 }
		 }
		 else
		 {
			request.setAttribute( SessionKeys.ERROR_MESSAGES ,errors );
			returnVal = "packingSlipMenu";
		 }
		if( action.equalsIgnoreCase( "update" ) )
		{

			customerShippingId = packingSlipForm.getCustomerShippingId();
			ClientInfo clientInfo = new ClientInfo();
			clientInfo = UserDAO.getClientAddressInfo();
		
			packingSlipForm = NewPackingSlipDAO.loadPackingSlipById ( customerShippingId );
			packingSlipForm.setFinalInventoryDataMap( NewPackingSlipDAO.loadLotNumberDataByCustomerShippingId( packingSlipForm.getCustomerShippingId() ) );
            packingSlipForm.setFinalInventoryDataMapView ( packingSlipForm.getFinalInventoryDataMap() );
			packingSlipForm.setClientInfo( clientInfo );
			session.setAttribute ( "packingSlipForm", packingSlipForm );
			request.setAttribute ( "showMainMenu", "TRUE" );

			
		}	
		return ( mapping.findForward ( returnVal ) );
	}
	
	private HashMap validateForm ( PackingSlipForm packingSlipForm, HttpSession session ) throws ScalarException 
	{
		HashMap errors = new HashMap();
		HashMap finalInventoryActionErrors = new HashMap();

		if ( packingSlipForm == null ) 
		{
			errors.put ( "Internal Server Error occured while processing the request", null );
		}
		
		finalInventoryActionErrors = ( HashMap ) session.getAttribute ( "FINAL_INVENTORY_ACTION_ERRORS" );
		session.removeAttribute ( "FINAL_INVENTORY_ACTION_ERRORS" );

		if ( ( finalInventoryActionErrors != null ) && ( finalInventoryActionErrors.isEmpty() == false ) )
		{
			errors.putAll ( finalInventoryActionErrors );
		}
		
		if( ( ( packingSlipForm.getLineitems() == null ) || ( packingSlipForm.getLineitems().isEmpty() == true ) ) )
		{
			errors.put( "Could not save Packing Slip, No lineItems available", null );	
		}
 
		try 
		{
			if ( ( packingSlipForm.getNoOfBoxes() != null ) && ( packingSlipForm.getNoOfBoxes().trim().length() != 0 ) ) 
			{
				Integer.parseInt ( packingSlipForm.getNoOfBoxes() );
			}
		}
		catch ( NumberFormatException nfe ) 
		{
			errors.put ( "No Of Boxes", "Not a number, please enter a valid number" );
		}

		if( ( ! packingSlipForm.isDropShippment() ) && ( ( packingSlipForm.getShipDate() == null ) || ( packingSlipForm.getShipDate().trim().length() == 0 ) ) )
		{
			errors.put ( "Ship Date", "Please enter a valid Ship Date" );
		}
		return errors;
	}
}